# -*- coding: utf-8 -*-
import Models
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write("In Get Mode")
        #latlng = GLatLng()
        #latlng.x = 31.2
        #latlng.y = 31.3
        #latlng.put()

        latlngs = db.GqlQuery("SELECT * FROM GLatLngModel")
        for latlng in latlngs:
            self.response.out.write(latlng.geopt)
##            self.response.out.write(latlng.geopt)
            self.response.out.write("<br/>")
            
##        string1 = "a,b,c,d"
##        ss = string1.split(",")
##        for i in range(len(ss)):
##            self.response.out.write(ss[i])          
    def post(self):
        user = users.get_current_user()
        
        #try to delete items first
        q = db.GqlQuery("SELECT * FROM GLatLngModel WHERE author = :author", author=user)
        results = q.fetch(100)
        for res in results:
            res.delete()

        #add items
        postpoints = self.request.POST.get('points')
        #self.response.out.write("Points:" + postpoints);
        points = postpoints.split("|")
        for i in range(len(points)-1):
            xy = points[i].split(",")
            latlng = Models.GLatLngModel()
            #self.response.out.write("Points:" + xy[0]);
            latlng.author = user
            geopt = db.GeoPt(xy[0],xy[1])
            latlng.geopt = geopt
            latlng.put()
        print ''' 
            <html> 
            <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    
  </head>
  <body>
      Save is done!
  </body> 
            </html> 
            ''' 

application = webapp.WSGIApplication([('/savedone', MainPage)],debug=True)

def main():
    run_wsgi_app(application)
if __name__ == "__main__":
    main()
